import loadData from '../services/itemListService';

export default {
    namespace:'itemList',

    state: {
        contentData:{},
        showContent:false,
        showLoading:false,
        showEmpty:false,
        showError:false,
        showFail:false,
        refreshing:false,
        emptyMsg:'',
        errorMsg:'',

        itemSearch: {
            brands:[],
            inStock:0,
            isSpecial:0,
            keyword:'',
            page:{
                currentPage:1,
                pageSize:10,
            },
            sort:6,
            type:4,
        },
        itemList:[],

        footerState:0,
        sortState:6
    },

    reducers: {
        // 初始化页面状态
        firstLoad(state, { payload }) {
            // 此处应将payload中的itemSearch存至state


            return {...state,
                ...payload,
                showLoading:true,
                showContent:false,
                showEmpty:false,
                showError:false,
                showFail:false,
                refreshing:false,

                itemList:[],
                footerState:0,
                sortState:6
            };
        },
        refresh(state, { payload }) {
            let itemSearch = state.itemSearch;
            itemSearch.page.currentPage = 1;

            return {...state,
                itemSearch: itemSearch,
                showLoading:false,
                showContent:true,
                showEmpty:false,
                showError:false,
                showFail:false,
                refreshing:true,
            };
        },
        loadNext(state, { payload }) {
            let itemSearch = state.itemSearch;
            itemSearch.page.currentPage ++ ;

            return {...state,
                itemSearch: itemSearch,
                footerState: 2,
            };
        },

        // 加载的结果
        loadSuccess(state, { payload }) {

            // 包含服务器返回的异常信息
            if (payload.head.code === '0') {
                // 此处应处理  contentData -> dataSource

                let itemList = payload.body.page.currentPage===1?
                    payload.body.itemList:
                    state.itemList.concat(payload.body.itemList);
                let isEmpty = itemList.length < 1;
                let footerState = payload.body.page.totalPage <= state.itemSearch.page.currentPage?3:1;
                isEmpty && (footerState = 0);
                return {...state,
                    contentData:payload,
                    showContent:true,
                    showLoading:false,
                    showEmpty:isEmpty,
                    showError:false,
                    showFail:false,
                    refreshing:false,

                    itemList: itemList,
                    footerState:footerState,
                };
            }
            else {
                // 服务器问题
                return {...state,
                    contentData:payload,
                    showLoading:false,
                    showContent:false,
                    showEmpty:false,
                    showFail:false,
                    showError:true,
                    refreshing:false,
                    errorMsg:payload.head.message,
                };
            }
        },

        // 网络层面, 非服务器问题
        loadFail(state, action) {

            return {...state,
                showLoading:false,
                showContent:false,
                showEmpty:false,
                showFail:true,
                showError:false,
                refreshing:false,
            };
        },


        changeSortReducer(state, {payload}) {

            let itemSearch = state.itemSearch;

            'sort' in payload && (itemSearch.sort = payload.sort);
            'isSpecial' in payload && (itemSearch.isSpecial = payload.isSpecial);

            return {...state,
                itemSearch: itemSearch,
                refreshing:true,
            };
        },

    },

    effects: {

        *firstLoadData({payload}, {put, call, select}) {
            yield put({
                type:'firstLoad',
                payload: payload
            });
            const result = yield call(loadData, {...payload});
            console.log('ItemListPage firstLoadData --->>> ', result);
            if (result.data) {
                yield put({
                    type:'loadSuccess',
                    payload: {...result.data}
                });
            }
            else {
                yield put({
                    type:'loadFail',
                })
            }

        },

    },

    subscriptions: {
        setup({dispatch}) {
            // register token


        }
    }
};
